/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package qlvbdq.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import qlvbdq.dconnection.SQLServerConnect;
import qlvbdq.dto.BaoCaoNoDTO;

/**
 *
 * @author tuan
 */
public class BaoCaoNoDAO {
    private static final String MaBaoCaoNo = "MaBCNO00";
    private static final int lengthMa = 8;
    
    public BaoCaoNoDAO() {
    }
    
    public String getNewMaBaoCaoNo() throws SQLException {
        String MaBCNO = "";
        int maxMa = 0;
        ResultSet rs = SQLServerConnect.getInstance().getDataFromStore("GetAllBaoCaoNoFromDataBase", null);
        while (rs.next()) {
            int temp = Integer.parseInt( rs.getString("MABAOCAO").substring(lengthMa) );
            if (maxMa < temp) {
                maxMa = temp;
            }
        }
        maxMa ++;
        MaBCNO = MaBCNO.concat(MaBaoCaoNo + maxMa);
        return MaBCNO;
    }
    
    public BaoCaoNoDTO getBaoCaoNoByMaBCNO(String MaBCNO) throws SQLException {
        Object[] objs = { MaBCNO };
        ResultSet result = SQLServerConnect.getInstance().getDataFromStore("GetBaoCaoNoFromDataBase( ? )", objs);
        BaoCaoNoDTO baocaoDT = new BaoCaoNoDTO();
        while (result.next()) {
            baocaoDT.setMaBaoCaoNo(result.getString("MABAOCAO"));
            baocaoDT.setNgayBaoCao(result.getDate("NGAYBAOCAO"));
        }
        return baocaoDT;
    }
    
    public ArrayList<BaoCaoNoDTO> getBaoCaoNoInTime(Date timeStart, Date timeEnd) throws SQLException {
        ArrayList<BaoCaoNoDTO> listBaoCao = new ArrayList<>();
        Object[] objs = { timeStart, timeEnd };
        ResultSet result = SQLServerConnect.getInstance().getDataFromStore("GetAllBaoCaoNoInTimeFromDataBase( ?, ? )", objs);
        BaoCaoNoDTO baocaoDT = new BaoCaoNoDTO();
        while (result.next()) {
            baocaoDT.setMaBaoCaoNo(result.getString("MABAOCAO"));
            baocaoDT.setNgayBaoCao(result.getDate("NGAYBAOCAO"));
            listBaoCao.add(baocaoDT);
        }
        return listBaoCao;
    }
    
    public int updateBaoCaoNo(BaoCaoNoDTO baocaoNo) {
        Object[] objs = { baocaoNo.getMaBaoCaoNo(), baocaoNo.getNgayBaoCao()};
        return SQLServerConnect.getInstance().updateDataToDatabase("UpdateBaoCaoNoToDataBase(?, ?)", objs);
    }
    
    public int deleteAllBaoCaoNo() {
        return SQLServerConnect.getInstance().updateDataToDatabase("DeleteAllBaoCaoNoFromDataBase", null);
    }
    
    public int deleteBaoCaoNoByMaBC(String MaBC) {
        Object[] objs = { MaBC };
        return SQLServerConnect.getInstance().updateDataToDatabase("DeleteBaoCaoNoFromDataBase( ? )", objs);
    }
    
    public int insertBaoCaoNo(BaoCaoNoDTO baocaoNo) {
        Object[] objs = { baocaoNo.getMaBaoCaoNo(), baocaoNo.getNgayBaoCao()};
        return SQLServerConnect.getInstance().updateDataToDatabase("InsertBaoCaoNoToDataBase(?, ?)", objs);
    }    
}
